<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>库存</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品编号</label>
                            <div class="layui-input-inline">
                                <input id="cid" type="text" name="cid" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input id="cname" type="text" name="cname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品类型</label>
                            <div class="layui-input-inline">
                                <input id="ctype" type="text" name="ctype" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="countTpl">
            {{#  if(d.count <100){ }}
            <span style="color: #FF0000;">{{d.count}}</span>{{d.mids.mbig}}
            {{#  } else { }}
            {{d.count+d.mids.mbig}}
            {{#  } }}
        </script>

        <script type="text/html" id="judgeTpl">
            {{#  if(d.count>100){ }}
            <span style="color: #00FA9A;">库存充足</span>
            {{#  } else { }}
            <span style="color: #FFA500;">请联系采购员补货</span>
            {{#  } }}
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../jquery-3.3.1.js" charset="UTF-8"></script>
<script>
        layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;

        table.render({
            elem: '#currentTableId',
            url: '/comm/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {width:20},
                {field: 'cid', width: 100, title: '库存编号', sort: true},
                {field: 'cname', minwidth: 90, title: '名称'},
                {field: 'tname', minwidth: 90, title: '类型',templet:function (d) {
                        return  d.tids.tname
                    }},
                {field: 'cprice', minwidth: 90, title: '进价', sort: true,templet:function (d) {
                        return d.cprice+"元/"+d.mids.mbig
                    }},
                {field: 'price', minwidth: 90, title: '单价', sort: true,templet:function (d) {
                        return (d.cprice/d.mids.scale).toFixed(2)+"元/"+d.mids.mlittle
                    }},
                {field: 'sprice', minwidth: 90, title: '售价', sort: true,edit:'text',templet:function (d) {
                        if (d.sprice==null||d.sprice===0){
                            return "<span style=\"color: #FF0000;\">未定价</span>"
                        }else {
                            return d.sprice+'元/'+d.mids.mlittle
                        }
                    }},
                {field: 'scale', minwidth: 90, title: '规格',templet:function (d) {
                        return d.mids.scale+d.mids.mlittle+"/"+d.mids.mbig
                    }},
                {field: 'count', minwidth: 200, title: '数量', sort: true,templet:'#countTpl'},
                {field: 'cid', minidth: 100, title: '供应商',templet:function (d) {
                        return  d.sids.firm
                    }},
                {field: 'count', minwidth: 90, title: '状态',templet:'#judgeTpl'},
            ]], page:true,
            limits: [5,10, 15]
            , limit: 10
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                }
                else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });
        function auto (){
            $.post(
                "/comm/sellUpdCount",{},function () {
                }
            )
        }
        auto()

            //监听单元格编辑
            table.on('edit(currentTableFilter)', function(obj){
                var value = obj.value //得到修改后的值
                    ,data = obj.data //得到所在行所有键值
                    ,field = obj.field; //得到字段
                $.post(
                    "/comm/updSprice",{
                        cid:data.cid,
                        sprice:util.escape(value)
                    },function () {
                        location.reload()
                    }
                )

            });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    cid:$("#cid").val(),
                    cname:$("#cname").val(),
                    ctype:$("#ctype").val(),
                }
            }, 'data');
            return false;
        });

            window.onload = function () {
                setInterval(function () {
                    window.location.reload()
                },1800000)
            }

    });
</script>

</body>
</html>